package cn.ycxy.lbl.search.controller;

import cn.ycxy.lbl.common.exception.BusinessException;
import cn.ycxy.lbl.common.exception.enums.ResponseEnum;
import cn.ycxy.lbl.common.restful.JsonPage;
import cn.ycxy.lbl.common.restful.R;
import cn.ycxy.lbl.pojo.search.domain.CommonDiscussPosts;
import cn.ycxy.lbl.pojo.search.domain.DealDiscussPosts;
import cn.ycxy.lbl.search.service.ISearchCommonService;
import cn.ycxy.lbl.search.service.ISearchDealService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "帖子搜索服务")
@RestController
@RequestMapping("/posts")
public class SearchController {

    @Autowired
    private ISearchCommonService searchCommonService;
    @Autowired
    private ISearchDealService searchDealService;

    @ApiOperation(value = "帖子搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "搜索关键字",name="keyword", example = "手机", required = true),
            @ApiImplicitParam(value = "页码",name="pageNo", example = "1", required = true),
            @ApiImplicitParam(value = "每页条数",name="pageSize", example = "5", required = true)
    })
    @GetMapping("/search/{type}")
    public R<Object> search(@ApiParam("帖子类型:0 1-失物、拼车搜索;3-交易帖子搜索") @PathVariable("type")Integer type,
                            String keyword, Integer pageNo, Integer pageSize){
        if (type == null){
            throw new BusinessException(ResponseEnum.BAD_REQUEST);
        }
        if (pageNo == null || pageNo < 0){
            pageNo = 1;
        }
        if (pageSize == null || pageSize <= 0){
            pageSize = 10;
        }
        if (type == 3){     //交易帖子搜索
            return R.ok(searchDealService.search(keyword, pageNo - 1, pageSize));
        }else {     //0 1 失物、拼车搜索
            return R.ok(searchCommonService.search(keyword, pageNo - 1, pageSize));
        }
    }


}
